#!/bin/bash
# API测试脚本 - 验证文档中的示例命令

BASE_URL="http://192.168.15.125:5000/test"

echo "🚀 开始测试Lidar控制系统API..."
echo "基础URL: $BASE_URL"
echo "========================================"

# 1. 测试设备状态查询
echo "📋 1. 测试设备状态查询"
echo "获取通道列表:"
curl -s "$BASE_URL/channel/list" | jq . 2>/dev/null || curl -s "$BASE_URL/channel/list"
echo ""

echo "获取云台状态:"
curl -s "$BASE_URL/ptz/list" | jq . 2>/dev/null || curl -s "$BASE_URL/ptz/list"
echo ""

echo "获取继电器状态:"
curl -s "$BASE_URL/relay/list" | jq . 2>/dev/null || curl -s "$BASE_URL/relay/list"
echo ""

# 2. 测试云台控制
echo "🎯 2. 测试云台控制"
echo "控制云台旋转到90度:"
response=$(curl -s -X POST "$BASE_URL/ptz/sendSpinCmd" \
  -H "Content-Type: application/json" \
  -d '{"channelName":"0000000000000000","address":30,"direction":"PAN","angle":90}')
echo "响应: $response"
sleep 2

echo "控制云台旋转到180度:"
response=$(curl -s -X POST "$BASE_URL/ptz/sendSpinCmd" \
  -H "Content-Type: application/json" \
  -d '{"channelName":"0000000000000000","address":30,"direction":"PAN","angle":180}')
echo "响应: $response"
sleep 2

echo "控制云台归零:"
response=$(curl -s -X POST "$BASE_URL/ptz/sendSpinCmd" \
  -H "Content-Type: application/json" \
  -d '{"channelName":"0000000000000000","address":30,"direction":"PAN","angle":0}')
echo "响应: $response"
echo ""

# 3. 测试继电器控制
echo "🔌 3. 测试继电器控制"
echo "打开继电器第1路:"
response=$(curl -s -X POST "$BASE_URL/relay/doControl" \
  -H "Content-Type: application/json" \
  -d '{"channelName":"d4ad2070b92f0000","address":1,"doNumber":1,"f":true}')
echo "响应: $response"
sleep 1

echo "关闭继电器第1路:"
response=$(curl -s -X POST "$BASE_URL/relay/doControl" \
  -H "Content-Type: application/json" \
  -d '{"channelName":"d4ad2070b92f0000","address":1,"doNumber":1,"f":false}')
echo "响应: $response"
echo ""

# 4. 测试错误处理
echo "⚠️ 4. 测试错误处理"
echo "测试无效通道:"
response=$(curl -s "$BASE_URL/ptz/invalid_channel/30/90")
echo "响应: $response"

echo "测试无效角度:"
response=$(curl -s "$BASE_URL/ptz/0000000000000000/30/999")
echo "响应: $response"
echo ""

# 5. 性能测试
echo "⚡ 5. 性能测试"
echo "测试API响应时间:"
start_time=$(date +%s.%N)
curl -s "$BASE_URL/channel/list" > /dev/null
end_time=$(date +%s.%N)
response_time=$(echo "$end_time - $start_time" | bc 2>/dev/null || echo "计算失败")
echo "响应时间: ${response_time}秒"

echo ""
echo "✅ API测试完成!"
echo "========================================"
echo "📖 完整API文档请查看: API_CONTROL_DOCUMENTATION.md"
echo "🌐 前端界面访问: http://192.168.15.125:5000"
